package com.quocnv.mediascanner;

import java.io.File;
import java.util.ArrayList;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import android.support.v4.app.FragmentActivity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class ShowSmbFileActivity extends FragmentActivity implements
		OnItemClickListener, OnClickListener
{
	String TAG = "ShowSmbFileActivity";
	ListView lvShowFile;
	SmbFileAdapter adapter;
	static int mShowType = 1;
	// String mPath = "smb://192.168.66.1/";
	String mPath = "smb://";
	ArrayList<SmbFile> mSmbFiles = new ArrayList<SmbFile>();

	Button btnShowAll;
	Button btnShowTree;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_file);

		mPath += (getIntent().getStringExtra("path") + "/");

		lvShowFile = (ListView) findViewById(R.id.lvShowFile);
		btnShowAll = (Button) findViewById(R.id.btnShowAll);
		btnShowTree = (Button) findViewById(R.id.btnShowTree);

		btnShowAll.setOnClickListener(this);
		btnShowTree.setOnClickListener(this);

		adapter = new SmbFileAdapter(this, mSmbFiles);
		lvShowFile.setAdapter(adapter);
		lvShowFile.setOnItemClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		try
		{
			SmbFile smbFile = (SmbFile) parent.getItemAtPosition(position);

			if (smbFile != null && smbFile.isDirectory())
			{
				adapter.setSmbFiles(SmbFileScanner.getInstance().getSmbFiles(
						smbFile.getCanonicalPath(),
						SmbFileFilterUtil.getMediaFilter()));
				adapter.notifyDataSetChanged();
				lvShowFile.setSelection(0);
			}
		} catch (SmbException e)
		{
			Toast.makeText(getApplicationContext(), "Không có quyền truy cập",
					Toast.LENGTH_LONG).show();

			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btnShowAll:
			mShowType = 0;
			updateListview();
			break;

		case R.id.btnShowTree:
			mShowType = 1;
			updateListview();
			break;

		default:
			break;
		}
	}

	private void updateListview()
	{
		// mPath = "mnt/usb/sda1";
		if (mPath == null)
		{
			adapter.clear();
			adapter.notifyDataSetChanged();
			return;
		}

		new SmbFileScanTask().execute(mPath, mShowType);
	}

	private class SmbFileScanTask extends
			AsyncTask<Object, Void, ArrayList<SmbFile>>
	{

		private ProgressDialog dialog = new ProgressDialog(
				ShowSmbFileActivity.this);

		@Override
		protected void onPreExecute()
		{
			dialog.setCancelable(true);
			dialog.setMessage("Scanning...");
			dialog.setIndeterminate(true);
			dialog.show();
			super.onPreExecute();
		}

		@Override
		protected ArrayList<SmbFile> doInBackground(Object... params)
		{
			String strPath = (String) params[0];
			int type = (Integer) params[1];

			ArrayList<SmbFile> files = new ArrayList<SmbFile>();
			try
			{
				if (type == 0)
					files = SmbFileScanner.getInstance().getAll(strPath,
							SmbFileFilterUtil.getMediaFilter());
				else
				{
					files = SmbFileScanner.getInstance().getSmbFiles(strPath,
							SmbFileFilterUtil.getMediaFilter());
				}
			} catch (SmbException e)
			{
				return null;
			}
			return files;
		}

		@Override
		protected void onPostExecute(ArrayList<SmbFile> result)
		{
			super.onPostExecute(result);
			if (this.dialog.isShowing())
				this.dialog.dismiss();

			if (result == null)
			{
				Toast.makeText(getApplicationContext(),
						"Không có quyền truy cập", Toast.LENGTH_LONG).show();
				return;
			}

			adapter.setSmbFiles(result);
			adapter.notifyDataSetChanged();
			lvShowFile.setSelection(0);
		}

	}
}
